﻿using System.Collections.Generic;

namespace xxpl1.Connection
{
    public class ConnectioPool
    {
        private readonly string server;
        private readonly LinkedList<Connection> connections = new LinkedList<Connection>();

        public ConnectioPool(string server)
        {
            this.server = server;
        }

        public Connection FindConnection()
        {
            lock (connections)
            {
                if (connections.Count == 0)
                {
                    return new Connection(this, server);
                }

                Connection connection = connections.First.Value;
                connections.RemoveFirst();
                return connection;
            }
        }

        public void ReleaseConnection(Connection connection)
        {
            lock (connections)
            {
                connections.AddFirst(connection);
            }
        }
    }

}
